package com.zmn.oms.api.controller.third.pinduoduo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.zmn.oms.common.exception.chan.ChannelFacadeException;
import com.zmn.oms.common.utils.RequestUtil;
import com.zmn.oms.third.pinduoduo.PinDuoDuoConsts;
import com.zmn.oms.third.pinduoduo.dto.api.PinDuoDuoResponse;
import com.zmn.oms.third.pinduoduo.dto.api.PinDuoDuoReturnResponse;
import com.zmn.oms.third.pinduoduo.service.PinDuoDuoFacadeService;
import com.zmn.oms.third.pinduoduo.service.PinDuoDuoService;
import com.zmn.oms.third.pinduoduo.util.PinDuoDuoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 类描述：拼多多对外接口
 *
 * @author wangxiaokun
 * @since 2020/5/29 15:00
 */
@RestController
@RequestMapping("/pddService")
@Slf4j
public class PinDuoDuoController {

    @Autowired
    PinDuoDuoService pinDuoDuoService;
    @Autowired
    private PinDuoDuoFacadeService pinDuoDuoFacadeService;

    /**
     * 拼多多访问路径-POST
     * @param request
     * @return com.zmn.oms.third.pinduoduo.dto.api.PinDuoDuoResponse
     */
    @RequestMapping(value = "/agreement/syncOrderMsg", method = RequestMethod.POST)
    public PinDuoDuoReturnResponse syncOrderMsg(HttpServletRequest request) {
        String paramsStr = RequestUtil.getPostParameter(request);
        log.info("【拼多多】收到消息！Body:[{}]", paramsStr);

        PinDuoDuoResponse response = null;
        try {
            if (StringUtils.isBlank(paramsStr)) {
                throw new ChannelFacadeException(PinDuoDuoConsts.ZMN_RETURN_CODE_PARAM_ERROR, "参数为空");
            }

            Map<String, String> params = null;
            try {
                params = JSON.parseObject(paramsStr, new TypeReference<Map<String, String>>() {});
            } catch (Exception e) {
                throw new ChannelFacadeException(PinDuoDuoConsts.ZMN_RETURN_CODE_PARAM_ERROR, "解析参数失败");
            }
            // 验证签名
            if (!PinDuoDuoUtil.verifySign(params)) {
                throw new ChannelFacadeException(PinDuoDuoConsts.ZMN_RETURN_CODE_PARAM_ERROR, "签名验证失败");
            }

            response = pinDuoDuoFacadeService.handleSyncOrderMsg(params);
        } catch (ChannelFacadeException cfe) {

            // 发生异常或错误
            response = new PinDuoDuoResponse();
            response.setSuccess(false);
            response.setErrorCode(cfe.getCode());
            response.setErrorMsg(cfe.getMessage());
        }
        PinDuoDuoReturnResponse pinDuoDuoReturnResponse = new PinDuoDuoReturnResponse();
        pinDuoDuoReturnResponse.setResponse(response);

        log.info("【拼多多】返回给拼多多的消息！[{}]", JSON.toJSONString(pinDuoDuoReturnResponse));

        return pinDuoDuoReturnResponse;
    }

}
